import * as base from "./base.js";
import instance from './request.js';
import { $, $$ } from './base.js';

window.addEventListener('load', function () {
    base.navbox();
    base.avatarimg();
    //跳转到发布房源页面
    if (localStorage.getItem('identity') == 2) {
        $('.add_fangyuan').style.display = 'block';
    }

    instance.get('/collection', {
        headers: {
            'Authorization': window.localStorage.getItem('token'),
            'Content-Type': 'application/json',
        }
    }).then(response => {
        let wisharr = response.data.data;
        console.log(wisharr);

        if (wisharr.length) {

            wisharr.forEach(function (value, index, array) {
                let str = `
                <div class="del_wishbox_li" id="${wisharr[index].id}">
                <div class="del_wishboxmask"></div>
                        <div class="wishbox_li_m_close">
                        <div class="delete_wish_mask"></div>
                            <button aria-label="删除「哈哈哈」（未共享）" type="button" class="del_wishbox_li_but"><span class="i13tpwjr dir dir-ltr"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" aria-hidden="true" role="presentation" focusable="false" style="display: block; height: 12px; width: 12px; fill: currentcolor;"><path d="m14.3.3 1.4 1.4L9.43 8l6.29 6.3-1.42 1.4L8 9.43 1.7 15.7.3 14.29 6.57 8 .3 1.7 1.71.3 8 6.57z"></path></svg></span></button>
                        </div>
                        <div class="del_wishbox_li_t">
                            <div class="del_wishbox_li_tn">
                            </div>
                        </div>
                        <div class="del_wishbox_li_b">
                            <div class="del_wishbox_li_b_t">${wisharr[index].name}</div>
                            <div class="del_wishbox_li_b_b"><span>${wisharr[index].count}</span>心愿单项目</div>
                        </div>
                    </div>`
                $('.wishcreate_ul').insertAdjacentHTML('beforeend', str);
            })
        } else {
            $('.wishmain_detail').style.display = 'block';
        }
    })





    //图片加载不出来
    // $('.lodger_img img').addEventListener('error', function () {
    //     $('.lodger_img img').src = avatarUrl;
    // })
    // $('.editorbody_l img').addEventListener('error', function () {
    //     $('.editorbody_l img').src = avatarUrl;
    // })


    $('.wishcreate_ul').addEventListener('error', function (event) {
        if (event.target.tagName === 'IMG') {
            console.log(1);
            event.target.src = event.target.src;
        }
    });





    let wishid = 0;
    $('.wishcreate_ul').addEventListener('click', function (event) {
        const targetElement = event.target;
        console.log(event.target);

        if (targetElement.classList.contains('delete_wish_mask')) {
            // console.log(targetElement.getAttribute('id'));
            $('.delete_wish').style.display = 'block';
            $('.delete_wish').classList.add('visible');
            $('.delete_wish_name').innerHTML = targetElement.parentNode.parentNode.children[3].children[0].innerHTML;

            wishid = targetElement.parentNode.parentNode.getAttribute('id');

        }

        if (targetElement.classList.contains('del_wishboxmask')) {

            wishid = targetElement.parentNode.getAttribute('id');
            console.log(wishid);
            localStorage.setItem('collectionid', wishid);
            localStorage.setItem('collectionname', targetElement.parentNode.children[3].children[0].innerHTML);

            location.href = 'wishdetail.html';

        }



    });

    //删除心愿单
    $('.delete_wish_b_r').addEventListener('click', base.debounce(function () {
        instance({
            url: `/collection`,
            method: 'DELETE',
            data: {
                collectionId: wishid
            },
            headers: {
                'Authorization': window.localStorage.getItem('token'),
                'Content-Type': 'application/json',
            }
        }).then(response => {
            console.log(response);
            $('.delete_wish').style.display = 'none';

            const container = document.querySelector('.wishcreate_ul');
            const liElement = container.querySelector(`[id="${wishid}"]`);
            liElement.remove();

            base.tooltipBox('', '删除成功!', '1');
        }).catch(error => {
            console.error(error)
        })
    })
    )


    // 关闭删除心愿单
    $('.delete_wish .delete_wish_close').addEventListener('click', function () {
        $('.delete_wish').classList.add('reverse-animation');

        // 等待动画结束后移除.visible和.reverse-animation类名
        setTimeout(function () {
            $('.delete_wish').classList.remove('visible', 'reverse-animation');
            $('.delete_wish').style.display = 'none';
        }, 500);
    })
    $('.delete_wish_b_l').addEventListener('click', function () {
        $('.delete_wish .delete_wish_close').click();
    })




})


